package com.coffee.admin.service.supplier.impl;


import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.supplier.query.SupplierSystemDeployQuery;
import com.coffee.admin.api.supplier.result.SupplierSystemDeployResult;
import com.coffee.admin.api.supplier.service.SupplierSystemDeployService;
import com.coffee.admin.service.supplier.bean.SupplierSystemDeploy;
import com.coffee.admin.service.supplier.dao.SupplierSystemDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@DubboService(dynamic = true)
public class SupplierSystemDeployServiceImpl implements SupplierSystemDeployService {
    private final static Logger LOG = LoggerFactory.getLogger(SupplierSystemDeployServiceImpl.class);
    @Resource
    private SupplierSystemDeployDao supplierSystemDeployDao;

    @Override
    public Page<SupplierSystemDeployResult> getPager(SupplierSystemDeployQuery param, PagerInfo pr) {
        Page<SupplierSystemDeployResult> pager = new Page<SupplierSystemDeployResult>(pr.getStart(), pr.getPageSize());
        Integer count = supplierSystemDeployDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<SupplierSystemDeploy> list = supplierSystemDeployDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, SupplierSystemDeployResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(SupplierSystemDeployQuery param) {
        Integer count = supplierSystemDeployDao.count(param);
        return count;
    }

    @Override
    public SupplierSystemDeployResult getDetail(Integer id) {
        SupplierSystemDeploy item = supplierSystemDeployDao.findById(id);
        if (item == null) {
            return null;
        }
        SupplierSystemDeployResult result = new SupplierSystemDeployResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<SupplierSystemDeployResult> getList(SupplierSystemDeployQuery param, PagerInfo pr) {
        List<SupplierSystemDeploy> list = supplierSystemDeployDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, SupplierSystemDeployResult.class);
    }

    @Override
    public List<SupplierSystemDeployResult> getList(SupplierSystemDeployQuery param) {
        List<SupplierSystemDeploy> list = supplierSystemDeployDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SupplierSystemDeployResult.class);
    }

    @Override
    public List<SupplierSystemDeployResult> getList() {
        SupplierSystemDeployQuery param = new SupplierSystemDeployQuery();
        List<SupplierSystemDeploy> list = supplierSystemDeployDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SupplierSystemDeployResult.class);
    }


    @Override
    public void delete(Integer id) {
        supplierSystemDeployDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(SupplierSystemDeployResult result) throws BusinessException {
        SupplierSystemDeploy item = new SupplierSystemDeploy();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            supplierSystemDeployDao.insert(item);
        } else {
            SupplierSystemDeploy tmp = supplierSystemDeployDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            LOG.info("item====" + JSONObject.toJSONString(item));
            supplierSystemDeployDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, SupplierSystemDeployResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, SupplierSystemDeployResult> map = new HashMap<Integer, SupplierSystemDeployResult>();
        List<SupplierSystemDeploy> list = supplierSystemDeployDao.findByIds(StringUtil.setToList(idSet));
        List<SupplierSystemDeployResult> resultList = DTOUtils.convertList(list, SupplierSystemDeployResult.class);
        for (SupplierSystemDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, SupplierSystemDeployResult> getItemMap() {
        Map<Integer, SupplierSystemDeployResult> map = new HashMap<Integer, SupplierSystemDeployResult>();
        SupplierSystemDeployQuery param = new SupplierSystemDeployQuery();
        List<SupplierSystemDeploy> list = supplierSystemDeployDao.page(param, 0, Integer.MAX_VALUE);
        List<SupplierSystemDeployResult> resultList = DTOUtils.convertList(list, SupplierSystemDeployResult.class);
        for (SupplierSystemDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
